<?php

namespace app\index\controller;

class Bonus extends Home
{
    /**
     * 切记：必须是小写字母
     * @var array
     */
    protected $beforeActionList = [
        'check_load'
    ];

    /**
     * 初始化
     */
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 用户奖金生成列表
     */
    public function bonusCount()
    {
        $uid = is_login();
        $where = $this->getMap('count_date');
        $map = $where['map'];
        $maps = $where['maps'];
        $map['userid'] = $uid;

        /*查询结果*/
        $bonus_list = Db('bonus_count')->where($map)->order('count_date desc')->paginate(20, false, ['query' => $maps]);
        $page = $bonus_list->render();
        $list = $this->getAll($bonus_list);
        $this->assign('_list', $list);
        $this->assign('page', $page);

        $this->searchCondition($maps);

        $this->assign('title', lang('Bonus Details'));
        return $this->fetch();
    }

    /**
     * 奖金管理
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bonusList()
    {
        $uid = is_login();
        $user = Db('member')->where('uid', $uid)->find();
        $bonusTotal = Db('bonus_total')->where('userid', $uid)->find();
        $this->assign('user', $user);
        $this->assign('bonusTotal', $bonusTotal);
        $this->assign('title', lang('Bonus Management'));
        return $this->fetch();
    }

    /**
     * 每日奖金
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bonusDetail()
    {
        $type = input('bonus');
        switch ($type) {
            case 1:
                $title = BONUS1;
                break;
            case 2:
                $title = BONUS2;
                break;
            case 3:
                $title = BONUS3;
                break;
            case 4:
                $title = BONUS4;
                break;
            case 5:
                $title = BONUS5;
                break;
            default:
                $this->error(lang('Parameter illegal!'));
        }
        $uid = is_login();
        $bonus = 'bonus' . $type;
        $map[$bonus] = array('gt', 0);
        $map['userid'] = $uid;
        $lists = Db('bonus_count')->where($map)->select();

        $tax_rate = get_bonus_rule('tax_rate');
        $cp_rate = get_bonus_rule('cp_rate');
        $rate = 1 - $tax_rate * 0.01 - $cp_rate * 0.01;
        $this->assign('_list', $lists);
        $this->assign('bonus', $bonus);
        $this->assign('type', $type);
        $this->assign('rate', $rate);
        $this->assign('title', $title);
        return $this->fetch();
    }

    /**
     * 奖金生成明细
     */
    public function showMore()
    {
        $uid = is_login();//当前登陆用户id
        $where = $this->getMap('createtime');
        $map = $where['map'];
        $maps = $where['maps'];
        $usernumber = input('usernumber');
        $changetype = input('changetype', 0);
        //查询类型
        if (!empty($usernumber)) {
            $maps['usernumber'] = $usernumber;
            $map['userid'] = Db('member')->where('usernumber', $usernumber)->value('uid');
        }

        $maps['targetuserid'] = $map['targetuserid'] = $uid;

        $changetypes = config('changetype');
        $changetypes[0] = '全部';
        if ($changetype > 0) {
            $maps['changetype'] = $map['changetype'] = $changetype;
        } else {
            $map['changetype'] = array('in', range(1, 10));
        }

        $list = Db('money_change')->where($map)->order('id desc')->paginate(20, false, ['query' => $maps]);
        $page = $list->render();
        $this->assign('_list', $list);
        $this->assign('page', $page);

        $arr = [
            [
                'type' => 'input',
                'name' => 'usernumber',
                'inputid' => '',
                'readonly' => '',
                'title' => lang('Membership Verification'),
                'value' => $usernumber,
            ]
        ];

        $this->searchCondition($maps, $arr);
        $this->assign('title', lang('Bonus Details'));
        return $this->fetch();
    }

    /**
     * 手机版奖金明细
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function showMoreWap()
    {
        $uid = is_login();//当前登陆用户id
        $today = input('today');
        $beginTime = $today;
        $endTime = $today + 60 * 60 * 24;
        $changetype = input('type', 0);
        $map['createtime'] = [['egt', $beginTime], ['lt', $endTime]];
        $map['changetype'] = $changetype;
        $map['targetuserid'] = $uid;

        $list = Db('money_change')->where($map)->order('createtime desc')->paginate(20, false, ['query' => $map]);
        $page = $list->render();
        $this->assign('_list', $list);
        $this->assign('page', $page);

        $this->assign('title', lang('Bonus Details'));
        return $this->fetch();
    }
}